package com.seckill.util;

import lombok.Data;

/****
 * @Author:www.itheima.com
 * @Description:返回实体Bean
 * @Date  15:55
 *****/
@Data
//@ApiModel(description = "Result",value = "Result")
public class Result<T> {
    //是否成功
    ////@ApiModelProperty(value="执行是否成功,true:成功,false:失败",required = true)
    private boolean flag;

    //状态码
    ////@ApiModelProperty(value="返回状态码," +
    //        "20000:成功," +
    //        "20001:失败," +
    //        "20002:用户名或密码错误," +
    //        "20003:权限不足," +
    //        "20004:远程调用失败," +
    //        "20005:重复操作," +
    //        "20006:没有对应的数据",
    //        required = true)
    private Integer code;

    //返回消息
    ////@ApiModelProperty(value="提示信息")
    private String message;

    //返回数据
    ////@ApiModelProperty(value="逻辑数据")
    private T data;

    public Result(boolean flag, Integer code, String message, Object data) {
        this.flag = flag;
        this.code = code;
        this.message = message;
        this.data = (T) data;
    }

    public Result(boolean flag, Integer code, String message) {
        this.flag = flag;
        this.code = code;
        this.message = message;
    }

    public static Result ok(ResultCode resultCode) {
        Result result = new Result();
        result.flag = true;
        result.code = resultCode.getCode();
        result.message = resultCode.getMsg();
        return result;
    }

    public static Result ok() {
        Result result = new Result();
        result.flag = true;
        result.code = ResultCode.SUCCESS.getCode();
        result.message = ResultCode.SUCCESS.getMsg();
        return result;
    }

    public static Result ok(Object data) {
        Result result = new Result();
        result.flag = true;
        result.data=data;
        result.code = ResultCode.SUCCESS.getCode();
        result.message = ResultCode.SUCCESS.getMsg();
        return result;
    }

    public static Result error(ResultCode resultCode) {
        Result result = new Result();
        result.flag = false;
        result.code = resultCode.getCode();
        result.message = resultCode.getMsg();
        return result;
    }

    public static Result error(ResultCode resultCode,Object data) {
        Result result = new Result();
        result.flag = false;
        result.code = resultCode.getCode();
        result.message = resultCode.getMsg();
        result.data=data;
        return result;
    }

    public static Result error() {
        Result result = new Result();
        result.flag = false;
        result.code = ResultCode.FAIL.getCode();
        result.message = ResultCode.FAIL.getMsg();
        return result;
    }

    public static Result error(String msg) {
        Result result = new Result();
        result.flag = false;
        result.code = ResultCode.FAIL.getCode();
        result.message = msg;
        return result;
    }

    public Result() {
        this.flag = true;
        this.code = ResultCode.SUCCESS.getCode();
        this.message = ResultCode.SUCCESS.getMsg();
    }
}
